package apollo.generator.modules.service.impl;

import apollo.generator.common.exception.CustomException;
import apollo.generator.common.utils.LoginUtils;
import apollo.generator.common.utils.PageUtils;
import apollo.generator.common.utils.Query;
import apollo.generator.modules.dao.TemplateDao;
import apollo.generator.modules.entity.TemplateEntity;
import apollo.generator.modules.enums.EnableEnum;
import apollo.generator.modules.service.TemplateService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <Description>
 * 模板配置
 * @author Jun Lee
 * @email tommy2015yoyo@163.com
 * @createDate 2020-07-02 10:21:00
 */
@Service("templateService")
public class TemplateServiceImpl extends ServiceImpl<TemplateDao, TemplateEntity> implements TemplateService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws CustomException {
        IPage<TemplateEntity> page = this.page(
                new Query<TemplateEntity>().getPage(params),
                new QueryWrapper<TemplateEntity>()
                        .eq("create_user_id", LoginUtils.getUserId())
        );

        return new PageUtils(page);
    }

    @Override
    public void enable(Long id, Integer enable, Long userId) {
        UpdateWrapper<TemplateEntity> wrapper = new UpdateWrapper<>();
        if (EnableEnum.ENABLE.getCode().equals(enable)) {
            wrapper.eq("create_user_id",userId).set("is_enable",EnableEnum.DISABLE.getCode());
            update(wrapper);
        }
        wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id).set("is_enable",enable);
        update(wrapper);
    }

}